home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / OOFILE / PhoneControl OOFILE sample / CMainWindow.cp < prev    next >
Encoding:
Text File  |  1995-12-09  |  7.4 KB  |  351 lines  |  [TEXT/MMCC]

  1. // CMainWindow.cp -- window methods
  2. // Created 01/01/95 12:01 PM by AppMaker
  3.  
  4. #include "CMainWindow.h"
  5. #include "PhoneControl_OOFILE.h"
  6.  
  7. #include <UReanimator.h>
  8. #include <LStream.h>
  9. #include <LListBox.h>
  10. #include <LStdControl.h>
  11. #include <LTabGroup.h>
  12. #include <LEditField.h>
  13. #include <LString.h>
  14. #include <UDesktop.h>
  15. #include <PP_KeyCodes.h>
  16. #include <PP_Messages.h>
  17. #include "LStrTableView.h"
  18. #include <LTableMonoGeometry.h>
  19. #include <LTableMultiSelector.h>
  20.  
  21. #include "stSetCursor.h"
  22.  
  23. #define PPob_MainWindowID    200
  24. #define RidL_MainWindowID    200
  25.  
  26.  
  27.  
  28. //----------
  29. //    This is the function you register with URegistrar to create a
  30. //    CMainWindow from a resource
  31.  
  32. CMainWindow*
  33. CMainWindow::CreateMainWindowStream(
  34.     LStream    *inStream)
  35. {
  36.     return (new CMainWindow(inStream));
  37. }
  38.  
  39. //----------
  40. CMainWindow::CMainWindow()
  41. {
  42. }
  43.  
  44. //----------
  45. CMainWindow::CMainWindow(
  46.     LStream    *inStream)
  47.         : LWindow(inStream)
  48. {
  49. }
  50.  
  51. //----------
  52. CMainWindow::~CMainWindow()
  53. {
  54. }
  55.  
  56. //----------
  57. //    This member function gets called once the containment hierarchy that contains
  58. //    this pane has been built. It gives us a chance to get data members for
  59. //    interesting subviews, and to do other operations now that our subviews exist.
  60. void
  61. CMainWindow::FinishCreateSelf()
  62. {
  63.     mMainList = (LStrTableView *)FindPaneByID('Main');
  64.     mFindButton = (LStdButton *)FindPaneByID('Fine');
  65.     mFindField = (LControlEnablingEditField *)FindPaneByID('Edit');
  66.     mLastNameButton = (LStdButton *)FindPaneByID('Lase');
  67.     mFirstNameButton = (LStdButton *)FindPaneByID('Fire');
  68.     mExtnButton = (LStdButton *)FindPaneByID('Extn');
  69.     mPagerButton = (LStdButton *)FindPaneByID('Pagr');
  70.     mDepartmentButton = (LStdButton *)FindPaneByID('Dept');
  71.     mTitleButton = (LStdButton *)FindPaneByID('Tite');
  72.     mShowAllButton = (LStdButton *)FindPaneByID('Shol');
  73.  
  74.     LTabGroup    *tabGroup = new LTabGroup(this);
  75.     mFindField->SetSuperCommander(tabGroup);    // becomes the active field
  76.  
  77.     UReanimator::LinkListenerToControls(this, this, RidL_MainWindowID);
  78.         // the purpose is to "connect" self to whatever controls
  79.         // that we want to "listen" to
  80.  
  81.     mMainList->AddListener(this);  // LStrTableView can't be included in RidL
  82.     
  83. // any additional initialization for your window:
  84.     mFindButton->Disable();
  85.     mShowAllButton->Disable();
  86.     mFindField->enablesControl(mFindButton);
  87.     mCurrentSortButton = mLastNameButton;
  88.     new LDefaultOutline(mFindButton);
  89.     mMainList->SetTableGeometry(new LTableMonoGeometry(mMainList, 104 /* width */, 11));
  90.     mMainList->SetTableSelector(new LTableMultiSelector(mMainList));
  91. }
  92.  
  93.  
  94. //----------
  95. void
  96. CMainWindow::DoFind()
  97. {
  98. // bit of a hack, if length is zero, select all records
  99. // that lets us call this routine after changing sort order
  100. // so we preserve the effect of the sort
  101.     stSetCursor showWatch;
  102.     
  103.     Str255 fieldContents;
  104.     mFindField->GetDescriptor(fieldContents);
  105.     if (fieldContents[0]=='\0')
  106.         mPhoneControl->selectAll();
  107.     else {
  108.         p2cstr(fieldContents);
  109.         mPhoneControl->search(mCurrentSortField->startsWith((char*)fieldContents));
  110.     }
  111.     mFindField->SelectAll();  // so overtyping will clear
  112.     RefreshBrowser();
  113.     mShowAllButton->Enable();
  114. }
  115.  
  116. //----------
  117. void
  118. CMainWindow::DoByLastName()
  119. {
  120.     ChangeSortTo(mLastNameButton, mPhoneControl->LastName());
  121. }
  122.  
  123. //----------
  124. void
  125. CMainWindow::DoByFirstName()
  126. {
  127.     ChangeSortTo(mFirstNameButton, mPhoneControl->FirstName());
  128. }
  129.  
  130. //----------
  131. void
  132. CMainWindow::DoByExtn()
  133. {
  134.     ChangeSortTo(mExtnButton, mPhoneControl->Extn());
  135. }
  136.  
  137. //----------
  138. void
  139. CMainWindow::DoByPager()
  140. {
  141.     ChangeSortTo(mPagerButton, mPhoneControl->Pager());
  142. }
  143.  
  144. //----------
  145. void
  146. CMainWindow::DoByDepartment()
  147. {
  148.     ChangeSortTo(mDepartmentButton, mPhoneControl->Department());
  149. }
  150.  
  151. //----------
  152. void
  153. CMainWindow::DoByTitle()
  154. {
  155.     ChangeSortTo(mTitleButton, mPhoneControl->Title());
  156. }
  157.  
  158. void
  159. CMainWindow::ChangeSortTo(LStdButton* newSortBtn, dbChar& sortField)
  160. {
  161.     Str255 newSortTitle;
  162.     mCurrentSortButton = newSortBtn;
  163.     newSortBtn->GetDescriptor(newSortTitle);
  164.     LStr255 newTitle(newSortTitle);
  165.     newTitle.Insert("Find ", 5, 0);
  166.     mFindButton->SetDescriptor(newTitle);    
  167.  
  168.     mPhoneControl->sortBy(sortField);
  169.     mCurrentSortField = &sortField;
  170. //    DoFind();  // repeats find, if any, on the current sort order
  171.     DoShowAll();
  172. }
  173.  
  174. //----------
  175. void
  176. CMainWindow::DoShowAll()
  177. {
  178. // only enabled if restricted the list
  179.     stSetCursor showWatch;;
  180.     
  181.     mPhoneControl->selectAll();
  182.     RefreshBrowser();
  183.     mShowAllButton->Disable();
  184. }
  185.  
  186.  
  187. //----------
  188. void
  189. CMainWindow::ListenToMessage(
  190.     MessageT    inMessage,
  191.     void        *ioParam)
  192. {
  193.     switch (inMessage) {
  194.     case 'Find':
  195.             DoFind();
  196.         break;
  197.  
  198.     case 'ByLe':
  199.             DoByLastName();
  200.         break;
  201.  
  202.     case 'ByFe':
  203.             DoByFirstName();
  204.         break;
  205.  
  206.     case 'ByEn':
  207.             DoByExtn();
  208.         break;
  209.  
  210.     case 'ByPr':
  211.             DoByPager();
  212.         break;
  213.  
  214.     case 'ByDt':
  215.             DoByDepartment();
  216.         break;
  217.  
  218.     case 'ByTe':
  219.             DoByTitle();
  220.         break;
  221.  
  222.     case 'Shol':
  223.             DoShowAll();
  224.         break;
  225.  
  226.     default:
  227.     if (!OOFILEhandlesMessage(inMessage, ioParam))
  228.       ; // do something
  229.         break;
  230.     }
  231. }
  232.  
  233. //----------
  234. Boolean
  235. CMainWindow::ObeyCommand(
  236.     CommandT    inCommand,
  237.     void        *ioParam)
  238. {
  239.     Boolean        cmdHandled = true;
  240.  
  241.     switch (inCommand) {
  242.  
  243.     // +++ Add cases here for the commands you handle
  244.     //        Remember to add same cases to FindCommandStatus below
  245.     //        to enable/disable the commands
  246.     default:
  247.     cmdHandled = OOFILEhandlesMessage(inCommand, ioParam);
  248.     if (!cmdHandled)
  249.             cmdHandled = LWindow::ObeyCommand(inCommand, ioParam);
  250.         break;
  251.     }
  252.  
  253.     return cmdHandled;
  254. }
  255.  
  256. //----------
  257. void
  258. CMainWindow::FindCommandStatus(
  259.     CommandT    inCommand,
  260.     Boolean        &outEnabled,
  261.     Boolean        &outUsesMark,
  262.     Char16        &outMark,
  263.     Str255        outName)
  264. {
  265.     outUsesMark = false;
  266.  
  267.     switch (inCommand) {
  268.  
  269.     // +++ Add cases here for the commands you handle
  270.     default:
  271.             LWindow::FindCommandStatus(inCommand, outEnabled,
  272.                                         outUsesMark, outMark, outName);
  273.         break;
  274.     }
  275. }
  276.  
  277. //----------
  278. Boolean
  279. CMainWindow::FocusDraw()
  280. {
  281.     Boolean        focused = LView::FocusDraw();
  282.  
  283.     if (focused) {
  284.         AuxWinHandle    awHndl;
  285.         CTabHandle        awCTable;
  286.         ColorSpec        contentSpec;
  287.  
  288.         GetAuxWin(GetMacPort(), &awHndl);
  289.         awCTable = (**awHndl).awCTable;
  290.         contentSpec = (**awCTable).ctTable [wContentColor];        // should search vs. index
  291.         ::RGBBackColor(&contentSpec.rgb);
  292.     }
  293.  
  294.     return focused;
  295. }
  296.  
  297.  
  298. void CMainWindow::SetDatabase(CdbPhoneControl* inPhoneControl)
  299. {
  300. // our copy of the database, if we need to change its state in our Do methods
  301.     mPhoneControl = inPhoneControl;    
  302.  
  303. // link view to controls created in FinishCreateSelf
  304.     dbView* theView = new dbView(inPhoneControl);
  305.     *theView  << inPhoneControl->LastName()  << inPhoneControl->FirstName()  << inPhoneControl->Title()  << inPhoneControl->Extn()  << inPhoneControl->Department()  << inPhoneControl->Pager() ;
  306.     AdoptView(theView);
  307.     BrowseViewWithTable(mMainList);  
  308.     inPhoneControl->sortBy(inPhoneControl->LastName());
  309.     mCurrentSortField = &(mPhoneControl->LastName());
  310. }
  311.  
  312.  
  313. Boolean
  314. CMainWindow::HandleKeyPress(
  315.     const EventRecord    &inKeyEvent)
  316. {
  317.     Boolean        keyHandled = false;
  318.     
  319.     switch (inKeyEvent.message & charCodeMask) {
  320.     
  321.         case char_Enter:
  322.         case char_Return:
  323.             mFindButton->SimulateHotSpotClick(kControlButtonPart);
  324.             keyHandled = true;
  325.             break;
  326.             
  327.         default:
  328.             keyHandled = LWindow::HandleKeyPress(inKeyEvent);
  329.             break;
  330.     }    
  331.     return keyHandled;
  332. }
  333.  
  334.  
  335. // lightweight class to wrap creating CMainWindow
  336.  
  337. LWindow* CMainWindowFactory::makeWindow(const bool loadData) const
  338. {
  339.     stSetCursor showWatch;
  340.     
  341.     CdbPhoneControl* realTable = (CdbPhoneControl*) mTable;  // safe downcast
  342.        // would later use dynamic_cast to test the above
  343.     CMainWindow *win = (CMainWindow *)LWindow::CreateWindow(PPob_MainWindowID, mCommander);
  344.     win->SetDatabase(realTable);
  345.     if (loadData)
  346.         win->LoadBrowseData();
  347.     return (LWindow*) win;
  348. }
  349.  
  350.  
  351.